#include "WindowHandler.h"

WindowHandler::WindowHandler(void)
{

    windowWidth = 500;
    windowHeight = 500;
    size=windowWidth*windowHeight;

    render = new Render();

    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("");


}
WindowHandler::~WindowHandler(void)
{
    delete render;
}

void WindowHandler::display()
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    renderToWindow();
    glutSwapBuffers();
}

//Function for the rendering to the screen
void WindowHandler::renderToWindow()
{

  glEnable(GL_CULL_FACE);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();



  float pixels[windowWidth][windowHeight][4];
  float acc_pixels[windowWidth][windowHeight][4];
  float *tracedPixel;

  int samples = 5;
   for(int i=0; i<windowWidth;i++){
        for(int j=0; j<windowHeight;j++){
            for(int k = 0; k<samples; k++){
                tracedPixel = render->renderPixel(i, j);
                acc_pixels[i][j][0] += tracedPixel[0];
                acc_pixels[i][j][1] += tracedPixel[1];
                acc_pixels[i][j][2] += tracedPixel[2];
                acc_pixels[i][j][3] += tracedPixel[3];

            }

            pixels[i][j][0] = acc_pixels[i][j][0]/samples;
            pixels[i][j][1] = acc_pixels[i][j][1]/samples;
            pixels[i][j][2] = acc_pixels[i][j][2]/samples;
            pixels[i][j][3] = acc_pixels[i][j][3]/samples;


        }
     }


  glDrawPixels(windowWidth, windowHeight, GL_RGBA, GL_FLOAT, pixels);


}


void WindowHandler::init()
{
    glutInitWindowSize (windowWidth, windowHeight); // Set the width and height of the window
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("RayTracer Window");

}


void WindowHandler::idle()
{

}






